package org.minproject.my_library_back.util;

import java.io.*;
import java.util.Scanner;

public class RSAFile {

    public static String getInformation(String publicKey) {
        String filePath = "src/main/resources/static/rsa.txt"; // 替换为你的文件路径
        try {
            File file = new File(filePath); // 替换为您的文件路径
            Scanner scanner = new Scanner(file);

            while (scanner.hasNext()) {
                String line = scanner.nextLine();
                String[] words = line.split("\\s+"); // 使用空格作为分隔符
                if(words[0].equals(publicKey)) {
                    return words[1];
                }
            }

            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void setInformation(String publicKey, String content)
    {
        String information = publicKey + " " + content+"\n";
        String filePath = "src/main/resources/static/rsa.txt";
        try (FileWriter fw = new FileWriter(filePath, true); // 创建FileWriter，第二个参数为true表示追加模式
             BufferedWriter bw = new BufferedWriter(fw)) {
            bw.write(information); // 写入内容
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
